package com.teampower.idea.action;

import java.util.List;

import com.opensymphony.xwork2.ActionContext;
import com.teampower.idea.base.BaseAction;
import com.teampower.idea.pojo.TestUsers;
import com.teampower.idea.service.TestUsersService;

public class TestUsersAction extends BaseAction {

	private String id;// 主键id
	private String username;// 登录名
	private String password;// 登录密码
	private String permission;// 管理权限
	private String msg;// 登录密码
	private List list;// 登录密码
	private TestUsers users;

	// private int startIndex;//起始页
	// private int pageSize;//每页记录数

	public TestUsers getUsers() {
		return users;
	}

	public void setUsers(TestUsers users) {
		this.users = users;
	}

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public String getUsername() {
		return username;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPermission() {
		return permission;
	}

	public void setPermission(String permission) {
		this.permission = permission;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	// 用户登录
	public String login() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("Entering 'TestUsersAction.login' method");
		}
		String result = "listData";
		TestUsersService testUsersService = (TestUsersService) getBean("testUsersService");
		TestUsers users = null;
		users = testUsersService.loadOneUser(this.username, this.password);

		if (null != users) {
			// 获取ActionContext实例，通过它来访问Servlet API
			ActionContext context = ActionContext.getContext();
			context.getSession().put("uName", this.username);
		} else {
			msg = "登录失败，用户名或密码错!";
			result = ERROR;
		}
		return result;
	}

	// 查询所有数据
	public String list() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("Entering 'TestUsersAction.list' method");
		}
		String result = "list";
		TestUsersService testUsersService = (TestUsersService) getBean("testUsersService");
		List list = null;
		list = testUsersService.listData(1, 30);
		setList(list);
		return result;
	}

	// 加载单条数据
	public String load() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("Entering 'TestUsersAction.load' method");
		}
		String result = "load";
		TestUsersService testUsersService = (TestUsersService) getBean("testUsersService");
		TestUsers users = null;
		users = testUsersService.loadOneUser(this.id);
		setUsers(users);

		this.id = users.getId();
		this.username = users.getUsername();
		this.password = users.getPassword();
		this.permission = users.getPermission();

		return result;
	}

	// 更新单条数据
	public String update() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("Entering 'TestUsersAction.update' method");
		}
		String result = "listData";
		TestUsersService testUsersService = (TestUsersService) getBean("testUsersService");
		TestUsers users = new TestUsers();

		users.setId(this.id);
		users.setUsername(this.username);
		users.setPassword(this.password);
		users.setPermission(this.permission);

		testUsersService.updateOneUser(users);
		return result;
	}

	// 添加单条数据
	public String save() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("Entering 'TestUsersAction.save' method");
		}
		String result = "listData";
		TestUsersService testUsersService = (TestUsersService) getBean("testUsersService");
		TestUsers users = new TestUsers();
		users.setId(this.id);
		users.setUsername(this.username);
		users.setPassword(this.password);
		users.setPermission(this.permission);
		testUsersService.save(users);
		return result;
	}

	// 删除单条数据
	public String delete() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("Entering 'TestUsersAction.delete' method");
		}
		String result = "listData";
		TestUsersService testUsersService = (TestUsersService) getBean("testUsersService");
		TestUsers users = new TestUsers();
		users.setId(this.id);
		testUsersService.delete(users);
		return result;
	}

	// 退出登录
	public String exit() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("Entering 'TestUsersAction.exit' method");
		}
		String result = "login";
		ActionContext context = ActionContext.getContext();
		context.getSession().remove("uName");
		return result;
	}

}